import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { PayAppApi } from '#/api/pay/app';

import { h } from 'vue';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { InputUpload } from '#/components/upload';
import { getRangePickerDefaultProps } from '#/utils';

import { $t } from '#/locales'

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: $t('views.pay.app.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.pay.app.namePlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'Select',
      componentProps: {
        placeholder: $t('common.statusPlaceholder'),
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        allowClear: true,
      },
    },
    {
      fieldName: 'createTime',
      label: $t('common.createTime'),
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(
  onStatusChange?: (
    newStatus: number,
    row: PayAppApi.App,
  ) => PromiseLike<boolean | undefined>,
): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'appKey',
      title: $t('views.pay.app.appKey'),
      minWidth: 40,
    },
    {
      field: 'name',
      title: $t('views.pay.app.name'),
      minWidth: 40,
    },
    {
      field: 'status',
      title: $t('common.status'),
      align: 'center',
      minWidth: 40,
      cellRender: {
        attrs: { beforeChange: onStatusChange },
        name: 'CellSwitch',
        props: {
          checkedValue: CommonStatusEnum.ENABLE,
          unCheckedValue: CommonStatusEnum.DISABLE,
        },
      },
    },
    {
      title: $t('views.pay.app.alipay.config'),
      children: [
        {
          title: $t('views.pay.app.alipay.channel.app'),
          slots: {
            default: 'alipayAppConfig',
          },
        },
        {
          title: $t('views.pay.app.alipay.channel.pc'),
          slots: {
            default: 'alipayPCConfig',
          },
        },
        {
          title: $t('views.pay.app.alipay.channel.wap'),
          slots: {
            default: 'alipayWAPConfig',
          },
          minWidth: 10,
        },
        {
          title: $t('views.pay.app.alipay.channel.qr'),
          slots: {
            default: 'alipayQrConfig',
          },
        },
        {
          title: $t('views.pay.app.alipay.channel.bar'),
          slots: {
            default: 'alipayBarConfig',
          },
        },
      ],
    },
    {
      title: $t('views.pay.app.wx.config'),
      children: [
        {
          title: $t('views.pay.app.wx.channel.lite'),
          slots: {
            default: 'wxLiteConfig',
          },
        },
        {
          title: $t('views.pay.app.wx.channel.pub'),
          slots: {
            default: 'wxPubConfig',
          },
        },
        {
          title: $t('views.pay.app.wx.channel.app'),
          slots: {
            default: 'wxAppConfig',
          },
        },
        {
          title: $t('views.pay.app.wx.channel.native'),
          slots: {
            default: 'wxNativeConfig',
          },
        },
        {
          title: $t('views.pay.app.wx.channel.wap'),
          slots: {
            default: 'wxWapConfig',
          },
          minWidth: 10,
        },
        {
          title: $t('views.pay.app.wx.channel.bar'),
          slots: {
            default: 'wxBarConfig',
          },
        },
      ],
    },
    {
      title: $t('views.pay.app.wallet.config'),
      field: 'walletConfig',
      slots: {
        default: 'walletConfig',
      },
    },
    {
      title: $t('views.pay.app.mock.config'),
      field: 'mockConfig',
      slots: {
        default: 'mockConfig',
      },
    },
    {
      title: $t('common.action'),
      width: 140,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 应用新增/修改的表单 */
export function useAppFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: $t('views.pay.app.name'),
      component: 'Input',
      rules: 'required',
      componentProps: {
        placeholder: $t('views.pay.app.namePlaceholder'),
      },
    },
    {
      fieldName: 'appKey',
      label: $t('views.pay.app.appKey'),
      component: 'Input',
      rules: 'required',
      componentProps: {
        placeholder: $t('views.pay.app.appKeyPlaceholder'),
      },
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'RadioGroup',
      rules: z.number().default(CommonStatusEnum.ENABLE),
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'orderNotifyUrl',
      label: $t('views.pay.app.orderNotifyUrl'),
      component: 'Input',
      rules: 'required',
      componentProps: {
        placeholder: $t('views.pay.app.orderNotifyUrlPlaceholder'),
      },
    },
    {
      fieldName: 'refundNotifyUrl',
      label: $t('views.pay.app.refundNotifyUrl'),
      component: 'Input',
      rules: 'required',
      componentProps: {
        placeholder: $t('views.pay.app.refundNotifyUrl'),
      },
    },
    {
      fieldName: 'transferNotifyUrl',
      label: $t('views.pay.app.transferNotifyUrl'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.pay.app.transferNotifyUrlPlaceholder'),
      },
    },
    {
      fieldName: 'remark',
      label: $t('views.common.remark'),
      component: 'Textarea',
      componentProps: {
        rows: 3,
        placeholder: $t('views.common.remarkPlaceholder'),
      },
    },
  ];
}

/** 渠道新增/修改的表单 */
export function useChannelFormSchema(formType: string = ''): VbenFormSchema[] {
  const schema: VbenFormSchema[] = [];
  // 添加通用字段
  schema.push(
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      label: $t('views.pay.channel.appId'),
      fieldName: 'appId',
      component: 'Input',
      dependencies: {
        show: () => false,
        triggerFields: [''],
      },
    },
    {
      label: $t('views.pay.app.channel.code'),
      fieldName: 'code',
      component: 'Input',
      dependencies: {
        show: () => false,
        triggerFields: [''],
      },
    },
    {
      label: $t('views.pay.app.channel.feeRate'),
      fieldName: 'feeRate',
      component: 'InputNumber',
      rules: 'required',
      componentProps: {
        placeholder: $t('views.pay.app.channel.feeRatePlaceholder'),
        addonAfter: '%',
      },
      defaultValue: 0,
    },
    {
      label: $t('common.status'),
      fieldName: 'status',
      component: 'RadioGroup',
      rules: z.number().default(CommonStatusEnum.ENABLE),
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
  );
  // 根据类型添加特定字段
  if (formType.includes('alipay_')) {
    schema.push(
      {
        label: $t('views.pay.app.alipay.appId'),
        fieldName: 'config.appId',
        component: 'Input',
        rules: 'required',
        componentProps: {
          placeholder:  $t('views.pay.app.alipay.appIdPlaceholder'),
        },
      },
      {
        label: $t('views.pay.app.alipay.serverUrl'),
        fieldName: 'config.serverUrl',
        component: 'RadioGroup',
        rules: 'required',
        componentProps: {
          options: [
            {
              value: 'https://openapi.alipay.com/gateway.do',
              label: $t('views.pay.app.alipay.env.prod'),
            },
            {
              value: 'https://openapi-sandbox.dl.alipaydev.com/gateway.do',
              label: $t('views.pay.app.alipay.env.sandbox'),
            },
          ],
          buttonStyle: 'solid',
          optionType: 'button',
        },
      },
      {
        label: $t('views.pay.app.alipay.signType'),
        fieldName: 'config.signType',
        component: 'RadioGroup',
        rules: 'required',
        componentProps: {
          options: [
            {
              value: 'RSA2',
              label: 'RSA2',
            },
          ],
          buttonStyle: 'solid',
          optionType: 'button',
        },
        defaultValue: 'RSA2',
      },
      {
        label: $t('views.pay.app.alipay.mode.pkType'),
        fieldName: 'config.mode',
        component: 'RadioGroup',
        rules: 'required',
        componentProps: {
          options: [
            {
              value: 1,
              label: $t('views.pay.app.alipay.mode.pk'),
            },
            {
              value: 2,
              label: $t('views.pay.app.alipay.mode.cert'),
            },
          ],
          buttonStyle: 'solid',
          optionType: 'button',
        },
      },
      {
        label: $t('views.pay.app.alipay.privateKey'),
        fieldName: 'config.privateKey',
        component: 'Textarea',
        rules: 'required',
        componentProps: {
          placeholder: $t('views.pay.app.alipay.privateKeyPlaceholder'),
          rows: 3,
        },
      },
      {
        label: $t('views.pay.app.alipay.alipayPublicKey'),
        fieldName: 'config.alipayPublicKey',
        component: 'Textarea',
        rules: 'required',
        componentProps: {
          placeholder: $t('views.pay.app.alipay.alipayPublicKeyPlaceholder'),
          rows: 3,
        },
        dependencies: {
          show(values: any) {
            return values?.config?.mode === 1;
          },
          triggerFields: ['config.mode', 'mode', 'config'],
        },
      },
      {
        label: $t('views.pay.app.alipay.appCertContent'),
        fieldName: 'config.appCertContent',
        component: h(InputUpload, {
          inputType: 'textarea',
          textareaProps: { rows: 3, placeholder: $t('views.pay.app.alipay.appCertContentPlaceholder') },
          fileUploadProps: {
            accept: ['crt'],
          },
        }),
        rules: 'required',
        dependencies: {
          show(values: any) {
            return values?.config?.mode === 2;
          },
          triggerFields: ['config.mode', 'mode', 'config'],
        },
      },
      {
        label: $t('views.pay.app.alipay.alipayPublicCertContent'),
        fieldName: 'config.alipayPublicCertContent',
        component: h(InputUpload, {
          inputType: 'textarea',
          textareaProps: { rows: 3, placeholder: $t('views.pay.app.alipay.alipayPublicCertContentPlaceholder') },
          fileUploadProps: {
            accept: ['crt'],
          },
        }),
        rules: 'required',
        dependencies: {
          show(values: any) {
            return values?.config?.mode === 2;
          },
          triggerFields: ['config.mode', 'mode', 'config'],
        },
      },
      {
        label: $t('views.pay.app.alipay.rootCertContent'),
        fieldName: 'config.rootCertContent',
        component: h(InputUpload, {
          inputType: 'textarea',
          textareaProps: { rows: 3, placeholder: $t('views.pay.app.alipay.rootCertContentPlaceholder') },
          fileUploadProps: {
            accept: ['crt'],
          },
        }),
        rules: 'required',
        dependencies: {
          show(values: any) {
            return values?.config?.mode === 2;
          },
          triggerFields: ['config.mode', 'mode', 'config'],
        },
      },
      {
        label: $t('views.pay.app.alipay.encryptType'),
        fieldName: 'config.encryptType',
        component: 'RadioGroup',
        rules: 'required',
        componentProps: {
          options: [
            {
              value: 'NONE',
              label: $t('views.pay.app.alipay.encryptTypeNone'),
            },
            {
              value: 'AES',
              label: 'AES',
            },
          ],
          buttonStyle: 'solid',
          optionType: 'button',
        },
        defaultValue: 'NONE',
      },
      {
        label: $t('views.pay.app.alipay.encryptKey'),
        fieldName: 'config.encryptKey',
        component: 'Input',
        rules: 'required',
        dependencies: {
          show(values: any) {
            return values?.config?.encryptType === 'AES';
          },
          triggerFields: ['config.encryptType', 'encryptType', 'config'],
        },
      },
    );
  } else if (formType.includes('wx_')) {
    schema.push(
      {
        label: $t('views.pay.app.wx.appId'),
        fieldName: 'config.appId',
        help: $t('views.pay.app.wx.appIdHelpMessage'),
        component: 'Input',
        rules: 'required',
        componentProps: {
          placeholder: $t('views.pay.app.wx.appIdPlaceholder'),
        },
      },
      {
        label: $t('views.pay.app.wx.mchId'),
        fieldName: 'config.mchId',
        help: $t('views.pay.app.wx.mchIdHelpMessage'),
        component: 'Input',
        rules: 'required',
        componentProps: {
          placeholder: $t('views.pay.app.wx.mchIdPlaceholder'),
        },
      },
      {
        label: $t('views.pay.app.wx.apiVersion'),
        fieldName: 'config.apiVersion',
        component: 'RadioGroup',
        rules: 'required',
        componentProps: {
          options: [
            {
              label: 'v2',
              value: 'v2',
            },
            {
              label: 'v3',
              value: 'v3',
            },
          ],
          buttonStyle: 'solid',
          optionType: 'button',
        },
      },
      {
        label: $t('views.pay.app.wx.mchKey'),
        fieldName: 'config.mchKey',
        component: 'Input',
        rules: 'required',
        componentProps: {
          placeholder: $t('views.pay.app.wx.mchKeyPlaceholder'),
        },
        dependencies: {
          show(values: any) {
            return values?.config?.apiVersion === 'v2';
          },
          triggerFields: ['config.mode', 'mode', 'config'],
        },
      },
      {
        label: $t('views.pay.app.wx.keyContent'),
        fieldName: 'config.keyContent',
        component: h(InputUpload, {
          inputType: 'textarea',
          textareaProps: {
            rows: 3,
            placeholder: $t('views.pay.app.wx.keyContentPlaceholder'),
          },
          fileUploadProps: {
            accept: ['p12'],
          },
        }),
        rules: 'required',
        dependencies: {
          show(values: any) {
            return values?.config?.apiVersion === 'v2';
          },
          triggerFields: ['config.mode', 'mode', 'config'],
        },
      },
      {
        label: $t('views.pay.app.wx.apiV3Key'),
        fieldName: 'config.apiV3Key',
        component: 'Input',
        rules: 'required',
        componentProps: {
          placeholder: $t('views.pay.app.wx.apiV3KeyPlaceholder'),
        },
        dependencies: {
          show(values: any) {
            return values?.config?.apiVersion === 'v3';
          },
          triggerFields: ['config.mode', 'mode', 'config'],
        },
      },
      {
        label: $t('views.pay.app.wx.privateKeyContent'),
        fieldName: 'config.privateKeyContent',
        component: h(InputUpload, {
          inputType: 'textarea',
          textareaProps: {
            rows: 3,
            placeholder: $t('views.pay.app.wx.privateKeyContentPlaceholder'),
          },
          fileUploadProps: {
            accept: ['pem'],
          },
        }),
        rules: 'required',
        dependencies: {
          show(values: any) {
            return values?.config?.apiVersion === 'v3';
          },
          triggerFields: ['config.mode', 'mode', 'config'],
        },
      },
      {
        label: $t('views.pay.app.wx.certSerialNo'),
        fieldName: 'config.certSerialNo',
        component: 'Input',
        help: $t('views.pay.app.wx.certSerialNoHelpMessage'),
        rules: 'required',
        componentProps: {
          placeholder: $t('views.pay.app.wx.certSerialNoPlaceholder'),
        },
        dependencies: {
          show(values: any) {
            return values?.config?.apiVersion === 'v3';
          },
          triggerFields: ['config.mode', 'mode', 'config'],
        },
      },
      {
        label: $t('views.pay.app.wx.publicKeyContent'),
        fieldName: 'config.publicKeyContent',
        component: h(InputUpload, {
          inputType: 'textarea',
          textareaProps: {
            rows: 3,
            placeholder: $t('views.pay.app.wx.publicKeyContentPlaceholder'),
          },
          fileUploadProps: {
            accept: ['pem'],
          },
        }),
        dependencies: {
          show(values: any) {
            return values?.config?.apiVersion === 'v3';
          },
          triggerFields: ['config.mode', 'mode', 'config'],
        },
      },
      {
        label: $t('views.pay.app.wx.publicKeyId'),
        fieldName: 'config.publicKeyId',
        component: 'Input',
        help: $t('views.pay.app.wx.publicKeyIdHelpMessage'),
        rules: 'required',
        componentProps: {
          placeholder: $t('views.pay.app.wx.publicKeyIdPlaceholder'),
        },
        dependencies: {
          show(values: any) {
            return values?.config?.apiVersion === 'v3';
          },
          triggerFields: ['config.mode', 'mode', 'config'],
        },
      },
    );
  }
  // 添加备注字段（所有类型都有）
  schema.push({
    label: $t(common.remark'),
    fieldName: 'remark',
    component: 'Input',
    componentProps: {
      placeholder: $t('common.remarkPlaceholder'),
    },
  });
  return schema;
}
